package ir.values;

import ir.values.users.instructions.Instruction;

public class FunctionIrIdGenerator {
    private final FunctionIr functionIr;
    public FunctionIrIdGenerator(FunctionIr functionIr) {
        this.functionIr = functionIr;
    }
    private int childId = 0;

    private int getNewChildId() {
        return childId++;
    }

    public void generateNames() {
        // 函数
        if (functionIr.isBuiltin()) {
            return;
        }

        // 参数
        for (Argument argument: functionIr.getArguments()) {
            argument.setId(getNewChildId());
        }

        // 基本块
        for (BasicBlock basicBlock: functionIr.getBasicBlocks()) {
            basicBlock.setId(getNewChildId());
            for (Instruction instruction: basicBlock.getInstructions()) {
                if (instruction.shouldGenerateId()) {
                    instruction.setId(getNewChildId());
                }
            }
        }
    }
}
